package models;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

public class Menu {
	
	private String MenuCategoryID;
	private String MenuCategoryName;
	private String MenuModuleID;
	private String MenuModuleName;
	private String FunctionCode;
	private String FunctionName;
	private String FunctionIndex;
	private String ParentCode;
	private boolean EndFlag;
	private String ObjectCode;
	private String FunctionID;
	private String Remark;
	private String SmallImageURL;
	private String LargeImageURL;
	private boolean IsHiddenFunction;
	private boolean isOpen;
	private boolean isActive;
	private String MenuEntranceID;
	
	private List<Menu> subMenu;
	
	public String getMenuCategoryID() {
		return MenuCategoryID;
	}
	public void setMenuCategoryID(String menuCategoryID) {
		MenuCategoryID = menuCategoryID;
	}
	public String getMenuCategoryName() {
		return MenuCategoryName;
	}
	public void setMenuCategoryName(String menuCategoryName) {
		MenuCategoryName = menuCategoryName;
	}
	public String getMenuModuleID() {
		return MenuModuleID;
	}
	public void setMenuModuleID(String menuModuleID) {
		MenuModuleID = menuModuleID;
	}
	public String getMenuModuleName() {
		return MenuModuleName;
	}
	public void setMenuModuleName(String menuModuleName) {
		MenuModuleName = menuModuleName;
	}
	public String getFunctionCode() {
		return FunctionCode;
	}
	public void setFunctionCode(String functionCode) {
		FunctionCode = functionCode;
	}
	public String getFunctionName() {
		return FunctionName;
	}
	public void setFunctionName(String functionName) {
		FunctionName = functionName;
	}
	public String getFunctionIndex() {
		return FunctionIndex;
	}
	public void setFunctionIndex(String functionIndex) {
		FunctionIndex = functionIndex;
	}
	public String getParentCode() {
		return ParentCode;
	}
	public void setParentCode(String parentCode) {
		ParentCode = parentCode;
	}
	public boolean isEndFlag() {
		return EndFlag;
	}
	public void setEndFlag(boolean endFlag) {
		EndFlag = endFlag;
	}
	public String getObjectCode() {
		return ObjectCode;
	}
	public void setObjectCode(String objectCode) {
		ObjectCode = objectCode;
	}
	public String getFunctionID() {
		return FunctionID;
	}
	public void setFunctionID(String functionID) {
		FunctionID = functionID;
	}
	public String getRemark() {
		return Remark;
	}
	public void setRemark(String remark) {
		Remark = remark;
	}
	public String getSmallImageURL() {
		return SmallImageURL;
	}
	public void setSmallImageURL(String smallImageURL) {
		SmallImageURL = smallImageURL;
	}
	public String getLargeImageURL() {
		return LargeImageURL;
	}
	public void setLargeImageURL(String largeImageURL) {
		LargeImageURL = largeImageURL;
	}
	public boolean isIsHiddenFunction() {
		return IsHiddenFunction;
	}
	public void setIsHiddenFunction(boolean isHiddenFunction) {
		IsHiddenFunction = isHiddenFunction;
	}
	public List<Menu> getSubMenu() {
		return subMenu;
	}
	public void setSubMenu(List<Menu> subMenu) {
		this.subMenu = subMenu;
	}
	public boolean isOpen() {
		return isOpen;
	}
	public void setOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}
	public boolean isActive() {
		return isActive;
	}
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}
	
	
	public String getMenuEntranceID() {
		return MenuEntranceID;
	}
	
	public void setMenuEntranceID(String menuEntranceID) {
		MenuEntranceID = menuEntranceID;
	}
	
	public static List<Menu> getMenus(String jsonMenuStr){
		List<Menu> menus = JSON.parseArray(jsonMenuStr, Menu.class);
		List<Menu> menuList = new ArrayList<Menu>();
		for(Menu menu : menus){
			if("".equals(menu.ParentCode)){
				menu.setSubMenu(getSubMenu(menu, menus));
				menuList.add(menu);
			}
			menu.isOpen = true;
		}
		return menuList;
	}
	
	public static List<Menu> getSubMenu(Menu menu, List<Menu> menuList){
		List<Menu> subMenus = new ArrayList<Menu>();
		for(Menu subMenu : menuList){
			if(subMenu.ParentCode.equals(menu.MenuModuleID)){
				if(!subMenu.EndFlag){
					subMenu.setSubMenu(getSubMenu(subMenu, menuList));
				}
				subMenus.add(subMenu);
			}
		}
		return subMenus;
	}
}
